package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.MbReportCard;
import com.menhai.personmanagement.entity.TjWordsTemplate;
import com.menhai.personmanagement.entity.vo.MbReportCardVo;
import com.menhai.personmanagement.mapper.TjWordsTemplateMapper;
import com.menhai.personmanagement.utils.UuidUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2022-01-12
 */
 @Service
public class ITjWordsTemplateService{

   @Autowired
   private TjWordsTemplateMapper tjWordsTemplateMapper;

   public List findWordsById(String id){
       return tjWordsTemplateMapper.findWordsById(id);

   }
   
   /**
    * 获取模板列表
    * @param TjWordsTemplate
    * @return
    */
   public IPage<TjWordsTemplate> getTemplateList(TjWordsTemplate tjWordsTemplate){
	   Page<MbReportCardVo> page = new Page<>(tjWordsTemplate.getPageIndex(),tjWordsTemplate.getPageSize());
	   return tjWordsTemplateMapper.getTemplateList(page, tjWordsTemplate);
   }
   
    /**
     * 新建模板
     * @param TjWordsTemplate
     * @return
     */
    @Transactional
 	public Map<String, Object> addTemplate(TjWordsTemplate tjWordsTemplate) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			tjWordsTemplate.setTemplateId(UuidUtil.uuid());
 			tjWordsTemplate.setSortNo(4);
 			tjWordsTemplate.setCenterId("1");
 			tjWordsTemplate.setTemplateStatus(1);
 			int iRet = tjWordsTemplateMapper.insert(tjWordsTemplate);		
 			if(iRet == 0){  //没有插入成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "插入成功的条数为0，操作模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "操作模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "操作模板失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
    
    /**
     * 删除模板
     * @param templateId
     * @return
     */
    @Transactional
 	public Map<String, Object> deleteTemplate(String templateId) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			int iRet = tjWordsTemplateMapper.delete(templateId);		
 			if(iRet == 0){  //没有删除成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "插入成功的条数为0，操作模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "操作模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "操作模板失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
    
    /**
     * 编辑模板
     * @param tjWordsTemplate
     * @return
     */
    @Transactional
 	public Map<String, Object> updateTemplate(TjWordsTemplate tjWordsTemplate) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			int iRet = tjWordsTemplateMapper.update(tjWordsTemplate);		
 			if(iRet == 0){  //没有更新成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "插入成功的条数为0，操作模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "操作模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "操作模板失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
}
